import java.util.Stack;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: wangj
 * Date: 2022-03-13
 * Time: 20:29
 */
class ListNode {
    public int val;
    public ListNode next;//类型是Node  null

    public ListNode(int val) {
        this.val = val;
    }
}
public class SingleLinkedList {
    public ListNode head;//为什么定义到这里
    public int usedSize;//记录当前链表中节点的个数

    /**
     * 先来了解一下链表的结构
     * 使用穷举的方式，创建链表
     */
    public void createList() {
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(34);
        ListNode node4 = new ListNode(45);
        ListNode node5 = new ListNode(56);
        //int a = 10;
        node1.next = node2;//？
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        node5.next = node2;

        this.head = node1;
        this.usedSize = 5;
    }
    //递归方式
    public void printList1(ListNode head){
        if(head.next!=null){
            printList1(head.next);
        }
        System.out.print(head.val+" ");
    }
    //栈方式
    public void pintList(ListNode head){
        ListNode cur=head;
        Stack<ListNode> stack=new Stack<>();
        //依次入栈
        while(cur!=null){
            stack.push(cur);
            cur=cur.next;
        }
        //出栈且打印
        while (!stack.empty()){
            System.out.print(stack.pop().val+" ");
        }
    }

    /**
     * 打印链表里面的元素
     */

    public void myToString() {
        ListNode cur = this.head;
        System.out.print("[ ");
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println("]");
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        ListNode cur = this.head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //得到单链表的长度-》尝试不用我们定义的usedSize
    public int size() {
        if (head == null) {
            return -1;
        }
        ListNode cur = this.head;
        int count = 0;
        while (cur != null) {
            cur = cur.next;
            count++;
        }
        return count;
    }


    //头插法
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if (this.head == null) {
            head = node;
        } else {
            node.next = head;
            head = node;
        }
        this.usedSize++;
    }

    //尾插法
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if (this.head == null) {
            head = node;
        } else {
            ListNode cur = this.head;
            while (cur.next != null) {
                cur = cur.next;
            }
            cur.next = node;
        }
    }

    //查找Index-1位置的节点的地址
    private ListNode searchIndex(int index) {
        ListNode cur = this.head;
        while (index - 1 != 0) {
            cur = cur.next;
            index--;
        }
        return cur;
    }

    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index, int data) {
        if (index < 0 || index > this.usedSize) {
            throw new RuntimeException("index不合法！");
        }
        if (index == 0) {
            addFirst(data);
            return;
        }
        if (index == this.usedSize) {
            addLast(data);
            return;
        }
        ListNode listNode = new ListNode(data);
        ListNode cur = searchIndex(index);
        listNode.next = cur.next;
        cur.next = listNode;
        this.usedSize++;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        if (this.head == null) {
            return;
        }
        if (this.head.val == key) {
            //只有1个节点
            this.head = this.head.next;//把头节点的引用置为空
            return;
        }
        ListNode prev = findPrevNode(key);
        if (prev == null) {
            throw new RuntimeException("不存在key节点！");
        }
        prev.next = prev.next.next;
        this.usedSize--;
    }

    private ListNode findPrevNode(int key) {
        ListNode prev = this.head;
        while (prev != null) {
            if (prev.next.val == key) {
                return prev;
            }
            prev = prev.next;
        }
        return null;
    }

    public void removeAllKey(int key) {
        if (head == null)
            return;
        ListNode prev = head;
        ListNode cur = prev.next;
        while (cur != null) {
            if (cur.val == key) {
                prev.next = cur.next;
                cur = cur.next;
                usedSize--;
            } else {
                prev = cur;
                cur = cur.next;
            }
        }
        if (head.val == key) {
            head = head.next;
            usedSize--;
        }
    }
    //删除所有值为key的节点
    public ListNode removeElements(int val) {
        if (head == null) {
            return null;
        }
        ListNode prev = head;
        ListNode cur = prev.next;
        while (cur != null) {
            if (cur.val == val) {
                prev.next = cur.next;
                cur = cur.next;
            } else {
                cur = cur.next;
                prev = prev.next;
            }
        }
        if (head.val == val) {
            head = head.next;
        }
        return head;
    }

    public ListNode reverseList() {
        if (head == null) {
            return null;
        }
        ListNode cur = head;
        ListNode curNext = cur.next;
        head.next = null;
        while (curNext != null) {
            head = curNext;
            curNext = curNext.next;
            head.next = cur;
            cur = head;

        }
        return head;
    }

    //找到链表的中间节点
    public ListNode middleNode() {
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }

    public void clear() {
        this.head = null;
    }

    /**
     * 找到单链表 倒数第K个节点
     * 要去 只遍历链表一遍
     *
     * @param k
     * @return
     */
    public ListNode findKthToTail(int k) {
        //k是否是合法的
        if (k <= 0 || head == null) {
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;
        while (k - 1 > 0) {
            fast = fast.next;
            k--;
            if (fast == null) {
                return null;
            }
        }
        while (fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }

    public boolean chkPalindrome() {
        if (head == null || head.next == null) {
            return true;
        }
        //1、找到链表的中间节点
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        //2、翻转
        ListNode cur = slow;
        ListNode curNext = cur.next;
        while (curNext != null) {
            slow = curNext;
            curNext = curNext.next;
            slow.next = cur;
            cur = slow;
        }

        //3、开始一个从头走，一个从尾巴走，直到相遇
        while (head != slow) {
            if (head.val != slow.val) {
                return false;
            }
            if (head.next == slow) {
                return true;
            }
            head = head.next;
            slow = slow.next;
        }
        return true;
    }

    //判断是否成环
    public boolean hasCycle() {
        if (head == null) {
            return false;
        }
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) {
                break;
            }
        }
        if (fast == null || fast.next == null) {
            return true;
        } else {
            return false;
        }
    }

    //返回成环位置节点
    public ListNode detectCycle() {
        if (head == null || head.next == null) {
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) {
                break;
            }
        }
        if (fast != null && fast.next != null) {
            slow = head;
            while (slow != fast) {
                slow = slow.next;
                fast = fast.next;
            }
            return slow;
        } else {
            return null;
        }

    }

    public ListNode partition(int x) {
        ListNode cur = head;
        ListNode bs = null;
        ListNode be = null;
        ListNode as = null;
        ListNode ae = null;
        while (cur != null) {
            if (cur.val < x) {
                if (bs == null) {
                    bs = cur;
                    be = cur;
                } else {
                    be.next = cur;
                    be = be.next;
                }
            } else {
                if (as == null) {
                    as = cur;
                    ae = cur;
                } else {
                    ae.next = cur;
                    ae = ae.next;
                }
            }
            cur = cur.next;
        }
        if (bs == null) {
            return as;
        }
        be.next = as;
        if (as != null) {
            ae.next = null;
        }
        return bs;
    }

    public ListNode deleteDuplicates() {
        if (head == null) {
            return null;
        }
        ListNode cur = head;
        while ( cur.next != null) {
            if (cur.val == cur.next.val) {
                cur.next = cur.next.next;
            }else {
            cur = cur.next;
            }
        }
        return head;
    }

}


